<style>
.pure_table_extra { width: 1100px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
.pure_table_extra tr.cancel td { text-decoration: line-through; }
.ui-autocomplete-loading { background: #fff url('../3rd_party/jquery/images/gg_load.gif') no-repeat right center; }
</style>

<?php 
$t2 = Utils::get_param('t2', '');
$o = Utils::get_param('o', 'id');
$q = Utils::get_param('q', 'DESC');
$pg = Utils::get_param('pg', 1);

$done = Utils::get_param('done', 0);
$enrolled = Utils::get_param('enrolled', 1);
$paid = Utils::get_param('paid', 0);
$unpaid = Utils::get_param('unpaid', 0);

$opts = new stdClass();
$opts->cd = 't.student_title__ LIKE :v2 AND t.is_trial = 0';
$opts->cdv = array(':v2' => '%' . $t2 . '%');

$opts->cd .= ' AND (';
if ($done == 0 && $enrolled == 0 && $paid == 0 && $unpaid == 0) {
  $opts->cd .= 'false';
} else if ($done == 1 && $enrolled == 0 && $paid == 0 && $unpaid == 0) {
  $opts->cd .= 't.is_current = 1';
} else if ($done == 0 && $enrolled == 1 && $paid == 0 && $unpaid == 0) {
  $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 1';
} else if ($done == 0 && $enrolled == 0 && $paid == 1 && $unpaid == 0) {
  $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 0 AND t.is_paid = 1';
} else if ($done == 0 && $enrolled == 0 && $paid == 0 && $unpaid == 1) {
  $opts->cd .= 't.is_current = 0 AND t.is_enrolled = 0 AND t.is_paid = 0';
} else if ($done == 1 && $enrolled == 1 && $paid == 0 && $unpaid == 0) {
  $opts->cd .= 't.is_current = 1 OR t.is_enrolled = 1';
} else if ($done == 1 && $enrolled == 0 && $paid == 1 && $unpaid == 0) {
  $opts->cd .= '(t.is_current = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
} else if ($done == 1 && $enrolled == 0 && $paid == 0 && $unpaid == 1) {
  $opts->cd .= '(t.is_current = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
} else if ($done == 1 && $enrolled == 1 && $paid == 1 && $unpaid == 0) {
  $opts->cd .= '(t.is_current = 1 OR t.is_enrolled = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
} else if ($done == 1 && $enrolled == 1 && $paid == 0 && $unpaid == 1) {
  $opts->cd .= '(t.is_current = 1 OR t.is_enrolled = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
} else if ($done == 1 && $enrolled == 0 && $paid == 1 && $unpaid == 1) {
  $opts->cd .= '(t.is_current = 1) OR (t.is_enrolled = 0)';
} else if ($done == 0 && $enrolled == 1 && $paid == 1 && $unpaid == 0) {
  $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR (t.is_paid = 1 AND t.is_enrolled = 0)';
} else if ($done == 0 && $enrolled == 1 && $paid == 0 && $unpaid == 1) {
  $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR (t.is_paid = 0 AND t.is_enrolled = 0)';
} else if ($done == 0 && $enrolled == 1 && $paid == 1 && $unpaid == 1) {
  $opts->cd .= '(t.is_current = 0 AND t.is_enrolled = 1) OR t.is_enrolled = 0';
} else if ($done == 0 && $enrolled == 0 && $paid == 1 && $unpaid == 1) {
  $opts->cd .= 't.is_enrolled = 0';
} else if ($done == 1 && $enrolled == 1 && $paid == 1 && $unpaid == 1) {
  $opts->cd .= 'true';
}
$opts->cd .= ')';

$data = $orm->get('pb_colg_enrollment', $o, $q, $pg, SIZE, $opts);

?>

<div class="pure-form">
  <input id="keywords2" type="text" placeholder="{Student}"  onkeypress="if(event.which == 13) search(1, 'id', 'DESC', 1);">
  <button onclick="search(1, 'id', 'DESC', 1);" class="pure-button">Search</button>
  <span id="types">
    <input onclick="search(1, 'id', 'DESC', 0);" type="checkbox" id="done" /><label for="done">Completed</label>
    <input onclick="search(1, 'id', 'DESC', 0);" type="checkbox" id="enrolled" /><label for="enrolled" style="margin-left: -5px;">Enrolled</label>
    <input onclick="search(1, 'id', 'DESC', 0);" type="checkbox" id="paid" /><label for="paid" style="margin-left: -5px;">Paid</label>
    <input onclick="search(1, 'id', 'DESC', 0);" type="checkbox" id="unpaid" /><label for="unpaid" style="margin-left: -5px;">Unpaid</label>
  </span>
</div>

<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="width: 40px;" class="ui-widget-header">Status</th>
      <th style="width: 120px;" class="ui-widget-header" onclick="return search(1, 'student_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Student</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return search(1, 'program_type', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Type</th>
      <th class="ui-widget-header" onclick="return search(1, 'program_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Program</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'start_academic', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">From</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'end_academic', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">To</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'fl_visa_expiry', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Visa</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'fl_insurance_expiry', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">INS</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'user_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">User</th>
      <th style="width: 40px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="enrls_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<?php include '../include/colg/inc.enrl.js.php'; ?>

<script type="text/javascript">
  /*<![CDATA[*/
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';
  var __pturl = '<?php echo ColgUtils::get_pturl($app_data); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __date = '<?php echo $date; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __language = '<?php echo PT_LANGUAGE; ?>';
  var __ncea = '<?php echo PT_NCEA; ?>';

  var __done = '<?php echo $done; ?>';
  var __enrolled = '<?php echo $enrolled; ?>';
  var __paid = '<?php echo $paid; ?>';
  var __unpaid = '<?php echo $unpaid; ?>';
  
  $(function() {
    $('#types').buttonset();

    $('#keywords2').autocomplete({
      source: __requrl + 'autocomplete2',
      minLength: 1,
      select: function(event, ui) {
        $('#keywords2').val(ui.item.value);
        search(1, 'id', 'DESC', 1);
      }
    });
    render();
  });
  
  function render() {
    $('#pagination').pagination({
      items: __data ? __data.t : 0,
      itemsOnPage: __data ? __data.s : 0,
      currentPage: __data ? __data.p : 0,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        search(pageNumber, __data.o, __data.q);
      }
    });
    
    render_data();
  };
  
  function render_data() {
    if (__data) {
      Url.insert_param('pg', __data.p);
      Url.insert_param('o', __data.o);
      Url.insert_param('q', __data.q);

      Url.insert_param('done', __done);
      Url.insert_param('enrolled', __enrolled);
      Url.insert_param('paid', __paid);
      Url.insert_param('unpaid', __unpaid);
    }

    if (__done == 1) {
      $('#done').prop('checked', 'checked');
    } else {
      $('#done').removeAttr('checked');
    }
    if (__enrolled == 1) {
      $('#enrolled').prop('checked', 'checked');
    } else {
      $('#enrolled').removeAttr('checked');
    }
    if (__paid == 1) {
      $('#paid').prop('checked', 'checked');
    } else {
      $('#paid').removeAttr('checked');
    }
    if (__unpaid == 1) {
      $('#unpaid').prop('checked', 'checked');
    } else {
      $('#unpaid').removeAttr('checked');
    }
    $('#types').buttonset('refresh');
    
    $('#enrls_body').empty();
    if (__data == null) {
      $('#enrls_body').append('<tr class="pure-table-even"><td colspan=10>Search for data</td></tr>');
      return;
    }
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#enrls_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + ((itm.is_cancel == 0) ? '' : ' cancel') + '">' + 
          '<td>' + ((itm.is_current == 0) ? ((itm.is_enrolled == 0) ? ((itm.is_paid == 0) ? 'Unpaid' : 'Paid') : 'Enrolled') : 'Completed') + '</td>' +
          '<td title="' + itm.student_title__ + '">' + Utils.get_short(itm.student_title__, 15) + '</td>' +
          '<td>' + itm.program_type + '</td>' +
          '<td title="' + itm.program_name + '">' + Utils.get_short(itm.program_name, 60) + '</td>' +
          '<td>' + itm.start_academic + '</td>' +
          '<td>' + itm.end_academic + '</td>' +
          '<td>' + itm.fl_visa_expiry + '</td>' +
          '<td>' + itm.fl_insurance_expiry + '</td>' +
          '<td>' + itm.user_title__ + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_enrl&id=' + itm.id + '\');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#enrls_body').append('<tr class="pure-table-even"><td colspan=10>None</td></tr>');
    }
  };

  function search(pg, o, q, reset) {
    if (reset == 1) {
      __done = 1;
      __enrolled = 1;
      __paid = 1;
      __unpaid = 1;
    } else {
      __done = $('#done').is(':checked') ? 1 : 0;
      __enrolled = $('#enrolled').is(':checked') ? 1 : 0;
      __paid = $('#paid').is(':checked') ? 1 : 0;
      __unpaid = $('#unpaid').is(':checked') ? 1 : 0;
    }
    
    var req = {
      term: $('#keywords').val(),
      term2: $('#keywords2').val(),
      pg: pg,
      q: q,
      o: o,
      done: __done,
      enrolled: __enrolled,
      paid: __paid,
      unpaid: __unpaid
    }
    Ajax.request(
      __requrl + 'search',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };

  /*]]>*/
</script>